package com.ruoyi.resume.mapper;

import com.ruoyi.resume.domain.model.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    // 查询所有分类
    @Select("SELECT id, parent_id, name, algorithm_identifier, args, level, is_last_level FROM category ORDER BY level ASC, id ASC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "name", property = "name"),
            @Result(column = "algorithm_identifier", property = "algorithmIdentifier"),
            @Result(column = "args", property = "args", javaType = String.class),
            @Result(column = "level", property = "level"),
            @Result(column = "is_last_level", property = "isLastLevel")
    })
    List<Category> selectAllCategories();

    // 查询指定分类
    @Select("SELECT id, parent_id, name, algorithm_identifier, args, level, is_last_level " +
            "FROM category WHERE id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "name", property = "name"),
            @Result(column = "algorithm_identifier", property = "algorithmIdentifier"),
            @Result(column = "args", property = "args", javaType = String.class),
            @Result(column = "level", property = "level"),
            @Result(column = "is_last_level", property = "isLastLevel")
    })
    Category selectCategoryById(Integer id);

    // 查询某分类的子分类
    @Select("SELECT id, parent_id, name, algorithm_identifier, args, level, is_last_level " +
            "FROM category WHERE parent_id = #{parentId} ORDER BY level ASC, id ASC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "name", property = "name"),
            @Result(column = "algorithm_identifier", property = "algorithmIdentifier"),
            @Result(column = "args", property = "args", javaType = String.class),
            @Result(column = "level", property = "level"),
            @Result(column = "is_last_level", property = "isLastLevel")
    })
    List<Category> selectCategoriesByParentId(Integer parentId);

    // 获取当前最大 ID
    @Select("SELECT IFNULL(MAX(id), 0) FROM category")
    Integer getMaxCategoryId();

    // 插入分类
    @Insert("INSERT INTO category (id, parent_id, name, algorithm_identifier, args, level, is_last_level) " +
            "VALUES (#{id}, #{parentId}, #{name}, #{algorithmIdentifier}, #{args}, #{level}, #{isLastLevel})")
    int insertCategory(Category category);

    // 删除分类
    @Delete("DELETE FROM category WHERE id = #{id}")
    int deleteCategoryById(Integer id);

    // 更新分类
    @Update("UPDATE category SET name = #{name}, algorithm_identifier = #{algorithmIdentifier}, " +
            "args = #{args}, level = #{level}, is_last_level = #{isLastLevel} WHERE id = #{id}")
    int updateCategory(Category category);

    // 修改分类的 is_last_level 状态
    @Update("UPDATE category SET is_last_level = #{isLastLevel} WHERE id = #{id}")
    int updateIsLastLevel(@Param("id") Integer id, @Param("isLastLevel") Boolean isLastLevel);

    /**
     * 根据 tagId 查询对应的 category_id
     */
    @Select("SELECT category_id FROM tag WHERE id = #{tagId}")
    Integer findCategoryIdByTagId(@Param("tagId") Integer tagId);

    /**
     * 根据 categoryId 查询分类信息
     */
    @Select("SELECT id, parent_id, algorithm_identifier FROM category WHERE id = #{categoryId}")
    Category findCategoryById(@Param("categoryId") Integer categoryId);

    /**
     * 根据 categoryId 查询算法地址
     */
    @Select("SELECT algorithm_identifier FROM category WHERE id = #{categoryId}")
    String findAlgorithmByCategoryId(@Param("categoryId") Integer categoryId);
}
